<?php
require_once('lanceur_spip.php'); // SPIP autoload
include_spip('functions/pdk_common');
//require_once(_DIR_PLUGIN_PDK.'classes/PdkRechercher.php');

class TestClassPdkRechercher extends SpipTest {
    var $rechercher;
	/**
     *  Some operations before all tests
     */
	function __construct() {
        //$this->rechercher = new PdkRechercher();           
        $this->rechercher = pdk_load_class('PdkRechercher', 'classes');
    }
    /**
     *  Run before each test
     */
    function setUp() {
        
    }
    
    /**
     *  Run after each test
     */
    function tearDown() {
        
    }
    
    /**
     *  Some operations after all tests
     */
	function __destruct() {
        
    }
    
    /**
     *  Test function CreateRegexp
     */
    function testCreateRegexp() {
        $regexp = $this->rechercher->CreateRegexp('тест   поиска');
        $this->assertEqual('тест.*поиска', $regexp);
        $regexp = $this->rechercher->CreateRegexp('тест+поиска');
        $this->assertEqual('тест.*поиска', $regexp);
    }
    
    /**
     *  Test function CreateRecherche
     */
    function testCreateRecherche() {
        $recherche =  $this->rechercher->CreateRecherche('%D1%82%D0%B5%D1%81%D1%82');
        $this->assertEqual('тест', $recherche);
        
        $recherche = $this->rechercher->CreateRecherche('%F2%E5%F1%F2');
        $this->assertEqual('', $recherche);
        
        $recherche = $this->rechercher->CreateRecherche('test');
        $this->assertEqual('test', $recherche);
    }
    
    /**
     *  Test function TableAllowed
     */
    function testTableAllowed() {
        $this->assertTrue($this->rechercher->TableAllowed('articles'));
        $this->assertFalse($this->rechercher->TableAllowed('test'));
    }
    
    /**
     *  Test function CreateQuery
     */
    function testCreateQuery() {
        $query_check = "UPPER(titre) REGEXP UPPER('тест') OR UPPER(texte) REGEXP UPPER('тест') OR UPPER(chapo) REGEXP UPPER('тест')";
        $query = $this->rechercher->CreateQuery('articles', 'тест', array('titre', 'texte', 'chapo'));
        $this->assertEqual($query_check, $query);
    }
    
    function testCreateBoucleWhereElement() {
        $array_check = array("'REGEXP'", "'UPPER(titre)'", "'UPPER(\'test\') OR UPPER(texte) REGEXP UPPER(\'test\')'");
        $array = $this->rechercher->CreateBoucleWhereElement('articles',array('titre','texte'), 'test');        
        $this->assertEqual($array_check, $array);
    }
}